package com.zez.controller;

import com.zez.common.util.PageResult;
import com.zez.common.util.RE;
import com.zez.entity.Book;
import com.zez.service.IBookService;
import com.zez.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zez
 * @since 2025-01-14
 */
@RestController
@RequestMapping("/book")
@CrossOrigin
public class BookController {
    @Autowired
    private IBookService bookService;

    @RequestMapping("/list")
    public RE getBook(){
        return RE.ok().data("list",bookService.list());
    }

    @GetMapping("/findbypage/{page}/{pagesize}")
    public RE findbypage(@PathVariable("page")Integer page,@PathVariable("pagesize") Integer pagesize){
        PageResult<Book> aa=bookService.findbypage(page,pagesize);
        return RE.ok().data("aa",aa);
    }

    @PostMapping("/search/{page}/{pagesize}")
    public RE search(@PathVariable("page")Integer page,@PathVariable("pagesize") Integer pagesize,@RequestBody(required = false) BookVO bookvo){
        PageResult<Book>aa=bookService.search(page,pagesize,bookvo);
        return RE.ok().data("aa",aa);
    }

    @PostMapping("/add")
    public RE add(@RequestBody Book book){
        boolean flag = bookService.add(book);
        return flag ? RE.ok().message("添加书本成功!") : RE.error().message("添加书本失败!");
    }

    @PutMapping("/update")
    public RE update(@RequestBody Book book){
        boolean flag = bookService.updateById(book);
        return flag ? RE.ok().message("修改书本成功!") : RE.error().message("修改书本失败!");
    }

    @DeleteMapping("/delete/{id}")
    public RE delete(@PathVariable("id") String id){
        String[] split = id.split(",");
        List<String> idlists = Arrays.asList(split);
        return bookService.removeByIds(idlists) ? RE.ok().message("删除书本成功!") : RE.error().message("删除书本失败!");
    }
}
